package com.bridgeintelligent.tag.customer.looklike;

import com.bridgeintelligent.tag.customer.model.CustomerModel;
import com.bridgeintelligent.tag.utils.PasswordHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "相似客群服务接口")
@AllArgsConstructor
@RestController
@RequestMapping("/api/lookLike")
@Slf4j
public class LookLikeController {


    private ILookLikeService lookLikeService;

    @ApiOperation("新增相似客群")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "lookLike", value = "相似客群信息")
    })
    @PostMapping("/add")
    public LookLikeResp addLookLike(@RequestBody LookLikeInfo lookLike) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(lookLike.getLookName())){
                return LookLikeResp.error("名字存在特殊字符");
            }
            //校验相似客群名是否重复
            boolean flag = lookLikeService.checkName(lookLike.getId(),lookLike.getLookName());
            if ( flag ){
                return LookLikeResp.error("客群名称重复，请修改客群名称后重新提交！");
            }
            lookLikeService.addLookLike(lookLike);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("相似客群添加失败：{}:",e);
            return LookLikeResp.error("相似客群添加失败!"+e.getMessage());
        }
    }

    @ApiOperation("修改相似客群")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "lookLike", value = "相似客群信息")
    })
    @PostMapping("/update")
    public LookLikeResp updateLookLike(@RequestBody LookLikeInfo lookLike) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(lookLike.getLookName())){
                return LookLikeResp.error("名字存在特殊字符");
            }
            //校验相似客群名是否重复
            boolean flag = lookLikeService.checkName(lookLike.getId(),lookLike.getLookName());
            if ( flag ){
                return LookLikeResp.error("客群名称重复，请修改客群名称后重新提交！");
            }
            lookLikeService.updateLookLike(lookLike);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("相似客群修改失败：{}:",e);
            return LookLikeResp.error("相似客群修改失败!"+e.getMessage());
        }
    }

    @ApiOperation("查看相似客群信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "客群ID")
    })
    @GetMapping("/findById")
    public LookLikeResp findById(String id) {
        try {
            LookLikeInfo data = lookLikeService.findById(id);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("查看相似客群信息失败：{}:",e);
            return LookLikeResp.error("查看相似客群信息失败");
        }
    }

    @ApiOperation("删除相似客群信息")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "客群ID",required = true)})
    @GetMapping("/del")
    public LookLikeResp del(String id) {
        try {
            lookLikeService.delById(id);
            return LookLikeResp.ok();
        }catch (Exception e){
            log.error("删除相似客群信息失败：{}:",e);
            return LookLikeResp.error("删除相似客群信息失败");
        }
    }

    @ApiOperation("查看相似客群列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "lookName", value = "相似客群名称",required = false),
            @ApiImplicitParam(paramType = "query", name = "isAnalysis", value = "相似客群状态",required = false),
            @ApiImplicitParam(paramType = "query", name = "stTime", value = "开始时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "edTime", value = "结束时间",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小",required = false)
    })
    @GetMapping("/findList")
    public LookLikeResp findList(String lookName,String isAnalysis,String stTime,String edTime,Integer pageNum,Integer pageSize) {
        try {
            LookLikePage<LookLikeInfo> data = lookLikeService.findList(lookName, isAnalysis, stTime, edTime, pageNum, pageSize);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("查看相似客群列表失败：{}:",e);
            return LookLikeResp.error("查看相似客群列表失败");
        }
    }

    @ApiOperation("修改相似客群的相关状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "isAnalysis", value = "相似客群状态",required = false),
            @ApiImplicitParam(paramType = "query", name = "seedStatus", value = "种子00未开始01处理中02处理成功03处理失败",required = false),
            @ApiImplicitParam(paramType = "query", name = "blackSeedStatus", value = "黑名单00未开始01处理中02处理成功03处理失败",required = false),
            @ApiImplicitParam(paramType = "query", name = "whiteSeedStatus", value = "白名单00未开始01处理中02处理成功03处理失败",required = false),
            @ApiImplicitParam(paramType = "query", name = "id", value = "相似客群ID",required = true)
    })
    @PostMapping("/updateDataStatus")
    public LookLikeResp updateDataStatus(String isAnalysis,String seedStatus,String blackSeedStatus,String whiteSeedStatus,String id) {
        try {
            lookLikeService.updateDataStatus(isAnalysis,seedStatus,blackSeedStatus,whiteSeedStatus,id);
            return LookLikeResp.ok();
        } catch (Exception e) {
            log.error("修改相似客群的相关状态失败：{}:",e);
            return LookLikeResp.error("修改相似客群的相关状态失败");
        }
    }

    @ApiOperation("启动生成并传输种子黑白名单数据文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "相似客群ID")
    })
    @GetMapping("/runData")
    public LookLikeResp runData(String id) {
        try {
            lookLikeService.readyData(id);
            return LookLikeResp.ok();
        }catch (Exception e){
            log.error("启动生成并传输种子黑白名单数据文件失败：{}:",e);
            return LookLikeResp.error("启动生成并传输种子黑白名单数据文件失败");
        }
    }

    @ApiOperation("处理失效的相似客群")
    @ApiImplicitParams({})
    @GetMapping("/makeExpire")
    public LookLikeResp makeExpire() {
        try {
            lookLikeService.makeExpire();
            return LookLikeResp.ok();
        }catch (Exception e){
            log.error("处理失效的相似客群失败：{}:",e);
            return LookLikeResp.error("处理失效的相似客群失败");
        }
    }

    @ApiOperation("查看相似客群标签特征列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "lookId", value = "相似客群ID",required = true),
            @ApiImplicitParam(paramType = "query", name = "dimName", value = "标签名称",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数",required = false),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "大小",required = false)
    })
    @GetMapping("/findCharacterList")
    public LookLikeResp findCharacterList(String lookId,String dimName,Integer pageNum,Integer pageSize) {
        try {
            LookLikePage<LookLikeCharacter> data = lookLikeService.findCharacterList(lookId,dimName,pageNum,pageSize);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("查看相似客群标签特征列表失败：{}:",e);
            return LookLikeResp.error("查看相似客群标签特征列表失败");
        }
    }

    @ApiOperation("查询相似客群选择种子黑白客群列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "customerName", value = "客群名称",required = false),
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "所属索引",required = false),
            @ApiImplicitParam(paramType = "query", name = "num", value = "客数",required = false)
    })
    @GetMapping("/lookCustomerList")
    public LookLikeResp lookCustomerList(String customerName,String indexId,String num) {
        try {
            List<CustomerModel> data = lookLikeService.lookCustomerList(indexId, customerName,num);
            return LookLikeResp.ok(data);
        }catch (Exception e){
            log.error("查询相似客群选择种子黑白客群列表失败：{}:",e);
            return LookLikeResp.error("查询相似客群选择种子黑白客群列表失败");
        }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList();
        BigDecimal l = new BigDecimal("3.87");
        BigDecimal m = new BigDecimal("-1.0");
        int n = 8;
        System.out.println("区间["+m+","+l+"],等分"+n+"份");
        BigDecimal offset = (l.subtract(m)).divide(new BigDecimal(String.valueOf(n)),5,BigDecimal.ROUND_HALF_UP);
        System.out.println(offset);
        BigDecimal min ;
        BigDecimal max;
        for (int i = 0; i < n; i++){
            if (i == n-1){
                min = m.add(new BigDecimal(String.valueOf(i)).multiply(offset)).setScale(2,BigDecimal.ROUND_HALF_UP);
                max =l.setScale(2,BigDecimal.ROUND_HALF_UP);
            }else{
                min = m.add(new BigDecimal(String.valueOf(i)).multiply(offset)).setScale(2,BigDecimal.ROUND_HALF_UP);
                max = m.add(new BigDecimal(String.valueOf(i+1)).multiply(offset)).setScale(2,BigDecimal.ROUND_HALF_UP);
            }
            list.add(min+"-"+max);
            System.out.println(min.doubleValue() +"------------"+max.doubleValue()+"--------"+(max.doubleValue()-min.doubleValue()));
        }
        sort(list);
        DecimalFormat df = new DecimalFormat("0.00");
        for (String s:list) {
            System.out.println(s);
            System.out.println(df.format(Double.parseDouble(s.split("-")[0])));
        }




    }

    static  void sort(List<String> data){
        data.sort((o1, o2) -> {
            Double v1 = Double.parseDouble(o1.split("-")[0]);
            Double v2 = Double.parseDouble(o2.split("-")[0]);
            return v2.compareTo(v1);
        });
    }

}